<template>
	<view class="u-page">
		<view class="box"></view>
		<view class="content">
			<view class="btn">
				<u-button type="primary" text="选择任务" @click="showHide" class="btn"></u-button>
			</view>
			<view class="show">
				<u--input border="surround" v-model="value" disabled disabledColor="#fff" inputAlign="center"
					fontSize="16px"></u--input>
			</view>
			<view class="switch">
				<view class="switchSon">
					优秀<u-switch v-model="switchBtnOptimal"></u-switch>
				</view>
				<view class="switchSon">
					合格<u-switch v-model="switchBtnQualified"></u-switch>
				</view>
				<view class="switchSon">
					不合格<u-switch v-model="switchBtnUnqualified"></u-switch>
				</view>
			</view>
			<view class="inputName">
				<u--textarea type="textarea" border="surround" v-model="reviewValue" placeholder="请输入评价" showWordLimit>>
				</u--textarea>
			</view>
			<view class="upload">
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:maxCount="3"></u-upload>
			</view>
			<view class="btn announce">
				<u-button type="primary" text="发布" @click="showHide" :disabled="bool"></u-button>
			</view>
			<!-- 任务选择器 -->
			<u-picker :show="show" ref="uPicker" :columns="columns" @confirm="confirm" @change="changeHandler"
				closeOnClickOverlay :loading="loading" @cancel="cancel"></u-picker>
		</view>
	</view>
</template>

<script>
	export default {
		computed: {
			columns() { // 选择器第一列 及 第一列首个所属二级信息
				return this.$store.state.columns;
			},
			columnData() { // 选择器第二列 --> 总信息
				return this.$store.state.columnData;
			}
		},
		data() {
			return {
				show: false, // 控制选择器的 "显示与隐藏"
				loading: false, // 加载效果
				value: '中国 深圳',
				// 开关选择器value
				switchBtnOptimal: false, // 优
				switchBtnQualified: false, // 合格
				switchBtnUnqualified: false, //不合格
				fileList1: [],	// 显示已上传的文件列表
				reviewValue: '',	//活动评价
				bool: true,
			}
		},
		watch: {
			//优
			switchBtnOptimal: function(val, oldVal) {
				if(val == true) {
					this.switchBtnQualified = false;
					this.switchBtnUnqualified = false;
				}
				// 判断是否启用发布按钮
				this.announce();
			},
			// 合格
			switchBtnQualified: function(val, oldVal) {
				if(val == true) {
					this.switchBtnOptimal = false;
					this.switchBtnUnqualified = false;
				}
				// 判断是否启用发布按钮
				this.announce();
			},
			// 不合格
			switchBtnUnqualified: function(val, oldVal) {
				if(val == true) {
					this.switchBtnOptimal = false;
					this.switchBtnQualified = false;
				}
				// 判断是否启用发布按钮
				this.announce();
			},
			// 图片
			fileList1: function(val, oldVal) {
				// 判断是否启用发布按钮
				this.announce();
			},
			// 评价
			reviewValue: function() {
				// 判断是否启用发布按钮
				this.announce();
			}
		},
		methods: {
			changeHandler(e) {
				const {
					columnIndex,
					value,
					values, // values为当前变化列的数组内容
					index,
					// 微信小程序无法将picker实例传出来，只能通过ref操作
					picker = this.$refs.uPicker
				} = e
				// 当第一列值发生变化时，变化第二列(后一列)对应的选项
				if (columnIndex === 0) {
					this.loading = true
					// 模拟网络请求
					uni.$u.sleep(1500).then(() => {
						picker.setColumnValues(1, this.columnData[index])
						this.loading = false
					})
				}
			},
			// 回调参数为包含columnIndex、value、values
			confirm(e) {
				// console.log('confirm', e)
				this.value = e.value[0] + " " + e.value[1];
				this.show = false
			},
			showHide() {
				this.show = true
			},
			cancel() {
				this.show = false
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].thumb)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
			// 关于是否启动发布按钮
			announce() {
				if(!((this.switchBtnOptimal || this.switchBtnQualified || this.switchBtnUnqualified) && this.value)) {
					// 禁用发布按钮
					this.bool = true
				} else if(this.switchBtnUnqualified) {
					if(this.fileList1.length > 0 && this.reviewValue.length > 0) {
						// 启用发布按钮
						this.bool = false
					} else {
					// 禁用发布按钮
					this.bool = true
					}
				} else {
					// 启用发布按钮
					this.bool = false
				}
			}
		}
	}
</script>

<style lang="scss">
	.u-page {
		padding: 0;
		margin: 0;

		// 顶部占位盒子
		.box {
			height: 60rpx;
			width: 100%;
		}

		.content {
			box-sizing: border-box;
			width: 90%;
			height: 100%;
			border: 1px solid #fff;
			border-radius: 20rpx;
			margin: 0rpx 5% 100rpx 5%;
			box-shadow: 0px 0px 8px #d5d5d6;

			// 选择任务按钮
			.btn {
				width: 200rpx;
				height: 100rpx;
				margin: 60rpx;
			}

			// input
			.show {
				width: 80%;
				height: 80rpx;
				margin: 100rpx auto;
			}

			// 开关选择按钮
			.switch {
				display: flex;
				justify-content: center;
				width: 100%;

				.switchSon {
					display: flex;
					width: 33%;
				}
			}

			// 任务显示
			.inputName {
				width: 94%;
				margin: 40rpx 10rpx;
			}
			// 上传
			.upload {
				display: flex;
				padding-left: calc((100% - 480rpx)/2);
				width: 100%;
				
			}
			// 发布按钮
			.announce {
				margin: 60rpx calc((100% - 200rpx)/2);
			}
		}
	}
</style>
